

import axios from 'axios'
let configuration = {
	url:process.env.VUE_APP_BASEURL
}
/**
 * 请求项目数据的请求体
 */
async function http({ url, method, param }) {
	const httpUrl = configuration.url + url
	if (method === 'GET') {
		httpUrl + jsonToGetParams(param)
	}
	const { data } = await axios({
		url: httpUrl,
		method: method ? method : 'GET',
		data: method === 'GET' ? {} : param,
		headers: {
			'content-type': 'application/json',
			token: localStorage.getItem('token') || ''
		}
	})
	return data
}
async function ask(e) {
	return http({
		url: e[0],
		method: e[1],
		param: e[2]
	})
}
export { ask }

/**
 * GET参数转字符串
 * @param jsonObj
 * @returns {string}
 */
function jsonToGetParams(jsonObj) {
	const params = Object.entries(jsonObj)
		.map(([key, value]) => `${key}=${value}`)
		.join('&')
	if (params.length >= 1) {
		return '?' + params
	} else {
		return params
	}
}
